import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2025-01-13
 * Time: 9:57
 */
public class WordPattern {
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> map = new HashMap<>();
        String[] strings = s.split(" ");
        if (strings.length != pattern.length()) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (map.containsKey(c) && !map.get(c).equals(strings[i])) {
                return false;
            } else if (!map.containsKey(c)) {
                if (map.containsValue(strings[i])) {
                    return false;
                }
                map.put(c, strings[i]);
            }
        }
        return true;
    }
}
